package RMBCaseConversion.weixian.main;

import java.math.BigDecimal;

public class RMBConversion {
    private String str[]={"个","十","百","千","万","十","百","千","亿","十","百","千","万"};
    private String value;
    private String upper[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    public RMBConversion(String string){
        System.out.println(toLowerCase(string));
    }
    public RMBConversion(BigDecimal number){
        System.out.println(toUpperCase(number));
    }
    private String toUpperCase(BigDecimal number){
        //保留小数点后两位
        number=number.setScale(2,   BigDecimal.ROUND_HALF_UP);
        //判断数值范围;
        if(number.compareTo(BigDecimal.ZERO)<0){
            //负数
            return "负"+toUpperCase(number.abs());
        }
        if(number.compareTo(BigDecimal.ZERO)==0){
            return "零圆";
        }
        //小数和整数分开处理
        value="";
        //处理整数部分
        truncToUpper(number.setScale(0,   BigDecimal.ROUND_DOWN));
        //处理小数部分
        fractionalPartToUpper(number.remainder( BigDecimal.ONE ));
        return value;
    }
    //处理整数部分
    private void truncToUpper(BigDecimal number){
        value=getChineseMoney(number)+"圆";
    }
    private String getChineseMoney(BigDecimal alb){
        alb=alb.setScale(0,   BigDecimal.ROUND_DOWN);
        int len = alb.toPlainString().length();
//        System.out.println(len+"-"+alb.toPlainString());
        switch (len){
            case 1:
                return  upper[alb.intValue()];
            case 2:
                return alb.remainder(BigDecimal.TEN).intValue() == 0 ? (alb.divide(BigDecimal.TEN).intValue()  == 1 ? "": upper[alb.divide(BigDecimal.TEN).intValue()])+"拾" : upper[alb.divide(BigDecimal.TEN).intValue()]+"拾" +upper[alb.remainder(BigDecimal.TEN).intValue()];
            case 3:
                if( alb.remainder(BigDecimal.valueOf(100)).intValue() == 0) {
                    return upper[alb.divide(BigDecimal.valueOf(100)).intValue()] + "佰";
                }
                if(String.valueOf(alb).charAt(1) == '0'){
                    return upper[alb.divide(BigDecimal.valueOf(100)).intValue()] + "佰"+upper[0]+upper[alb.remainder(BigDecimal.valueOf(100)).intValue()];
                }else{
                    return upper[alb.divide(BigDecimal.valueOf(100)).intValue()] + "佰" + getChineseMoney(alb.remainder(BigDecimal.valueOf(100)));
                }
            case 4:
                if( alb.remainder(BigDecimal.valueOf(1000)).intValue() == 0) {
                    return upper[alb.divide(BigDecimal.valueOf(1000)).intValue()] + "仟";
                }
                if(alb.toPlainString().charAt(1) == '0' && alb.toPlainString().charAt(2) == '0'){
                    return upper[alb.divide(BigDecimal.valueOf(1000)).intValue()] + "仟"+upper[0]+upper[alb.remainder(BigDecimal.valueOf(100)).intValue()];
                }else if(alb.toPlainString().charAt(1) == '0'){
                    return upper[alb.divide(BigDecimal.valueOf(1000)).intValue()] + "仟" + upper[0] + getChineseMoney(alb.remainder(BigDecimal.valueOf(100)));
                } else {
                    return upper[alb.divide(BigDecimal.valueOf(1000)).intValue()] + "仟"  + getChineseMoney(alb.remainder(BigDecimal.valueOf(1000)));
                }
            case 5:

            case 6:

            case 7:

            case 8:
                if(alb.remainder(BigDecimal.valueOf(10000)).compareTo(BigDecimal.ZERO)==0)return getChineseMoney(alb.divide(BigDecimal.valueOf(10000))) + "万";
                if(alb.remainder(BigDecimal.valueOf(10000)).compareTo(BigDecimal.valueOf(1000))==-1) return getChineseMoney(alb.divide(BigDecimal.valueOf(10000))) + "万零" + getChineseMoney(alb.remainder(BigDecimal.valueOf(10000)));
                return getChineseMoney(alb.divide(BigDecimal.valueOf(10000))) + "万" + getChineseMoney(alb.remainder(BigDecimal.valueOf(10000)));
            case 9:

            case 10:

            case 11:

            case 12:
                if(alb.remainder(BigDecimal.valueOf(10000*10000L)).compareTo(BigDecimal.ZERO)==0) return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000L))) + "亿";
                if(alb.remainder(BigDecimal.valueOf(10000*10000L)).compareTo(BigDecimal.valueOf(10000000L))==-1) return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000L))) + "亿零" + getChineseMoney(alb.remainder(BigDecimal.valueOf(10000*10000L)));
                return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000L))) + "亿" +  getChineseMoney(alb.remainder(BigDecimal.valueOf(10000*10000L)));
            case 13:

            case 14:

            case 15:
                if(alb.remainder(BigDecimal.valueOf(10000*10000*10000L)).compareTo(BigDecimal.ZERO)==0) return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000*10000L))) + "万忆";
                if(alb.remainder(BigDecimal.valueOf(10000*10000*10000L)).compareTo(BigDecimal.valueOf(100000000000L))==-1) return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000*10000L))) + "万亿零" + getChineseMoney(alb.remainder(BigDecimal.valueOf(10000*10000*10000L)));
                return getChineseMoney(alb.divide(BigDecimal.valueOf(10000*10000*10000L))) + "万" +  getChineseMoney(alb.remainder(BigDecimal.valueOf(10000*10000*10000L)));
            default:
                return "超出了范围";
        }
    }
    //处理小数部分-只有两位，直接处理
    private void fractionalPartToUpper(BigDecimal number){
        //变为整数
        number=number.multiply(BigDecimal.valueOf(100));
        int temp=number.intValue();
        if(temp>9){ //有角
            value=value+upper[temp/10]+"角";
            if(temp%10 == 0) return;    //仅有角 直接返回
        }else{  //无角
            if(temp%10 != 0){    //有分
                if(value.length()!=0){  //如果有圆，因为没有角，需要加”零“
                    value=value+"零";
                }
            }else return ;  //没有角 没有分 直接返回
        }
        value=value+upper[temp%10]+"分";
    }
    private BigDecimal toLowerCase(String str){
        String parts[]=str.split("圆|元");
        String integerPart=parts[0];
        String fractionalPart;
        if(parts.length>1){
            fractionalPart=parts[1];
        }else{
            fractionalPart="";
        }

        return new BigDecimal("1024");
    }
}
